<?php
namespace Home\Util;
require_once(THINK_PATH . "Library/Util/ChatPay/PayNotifyBasic.class.php");
use Util\ChatPay\PayNotifyBasic;



class PayNotify extends PayNotifyBasic {

    //微信回调处理函数
    public function overWriteCall($data) {
        //附加数据
        $Oid = $data['attach'];
        //初始化日志
        $filename = C('WXPAY_LOG') . '/' . date('Y-m-d') . '.log';
        $word = "微信支付回调函数\r\n微信商户订单数据：" . json_encode($data) . "\r\n";  //双引号会换行 单引号不换行
        file_put_contents($filename, $word, FILE_APPEND);
        $Owhere['order_id'] = $Oid;
        $OLS = Service('Order', 'order');
        $Odata = $OLS->find($Owhere);

        //用户可支付余额
        $YE_PAY_LS = Service('Pay','cost');
        $YECOST = $YE_PAY_LS->YE_Pay($Odata['user_id'], $Odata['order_money'], $Odata['order_id'],0);
        $Odata['order_ye_pay_money'] = $YECOST;
        $word = "微信支付回调函数\r\n平台订单数据：" . json_encode($Odata) . "\r\n";  //双引号会换行 单引号不换行
        file_put_contents($filename, $word, FILE_APPEND);
        //  订单号是否存在 是否待支付
        if ($Odata['order_type'] == 1 && (String)(((float)$Odata['order_money']-(float)$YECOST) * 100) == (String)$data['total_fee']) {
            $LS = Service('Pay', 'order');
            $OK = $LS->Pay($Odata);
            //下单通知
            $this->AddOrder_SendMsg($Oid);
            return $OK;
        } else {
            return false;
        }

    }

    /*
* 用户下单
* 管理员收到通知
* order_id 订单id
* top
* 1购买订单
* 2回收订单
*/

    public function AddOrder_SendMsg($order_id) {
        $LS = Service('M','order');
        $where['order_id'] = $order_id;
        $Order =  $LS->find($where);
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = 'lXm-BetKbFLVkjeekr0ulWSKoxx1WJcl-yj2ZuHsENs';//消息模板id
        $user = C('ADMIN_OPENID');
           //消息体信息
        $first = '客户下单买书啦，快去后台操作出库吧！';
        $keyword1 = '共'.$Order['order_num'].'本书';
        $keyword2 = $Order['order_book_money'];
        $keyword3 = $Order['order_address_name'].' '. $Order['order_phone'];
        $keyword4 = $Order['order_address'];
        $keyword5 = $Order['order_start_time'];
        $remark = '-->点击查看详情吧o(∩_∩)o ';
        $url =  'http://www.yanxiang.pw/orders/admin?order_id=' . $order_id;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1, $keyword2,$keyword3,$keyword4,$keyword5);
        foreach ($user as $k => $v) {
            $Send->init($APPID, $SECRETID, $v, $template_id, $url);
            $Send->Setfirst($first);
            $Send->Setremark($remark);
            $Send->Setdata($data);
            $OK = $Send->SendMsg();
        }
    }
}

?>